import {messages} from "share/common";
import React from "react";
import WrapFormItem from "components/wrap-form-item";
import { Row, Col } from "antd";
import Selector from 'components/selector'
import Chooser from 'components/chooser'

const hasValue = value => value && value.partnerCategory;

const onChangeType = (innerValue, onChange) => {
  onChange({
    partnerCategory: innerValue,
    partnerId: []
  });
};

export default {
  id: 'partner', label: messages('finance-4.key302')/*收款方*/,
  component: <WrapFormItem parser={value => value || {}}>
    {(onChange, value) => (
      <Row gutter={10}>
        <Col span={10}>
          <Selector
            type='value_list'
            entity={true}
            value={value.partnerCategory}
            params={{ type: 2107 }}
            onChange={innerValue => onChangeType(innerValue, onChange)}
          />
        </Col>
        <Col span={14} style={{ height: 32 }}>
          <Chooser
            type={ value.partnerCategory && value.partnerCategory.code === 'EMPLOYEE' ? 'user' : 'finance_vendor' }
            single
            disabled={ !value.partnerCategory }
            valueKey='id'
            labelKey={ value.partnerCategory &&  value.partnerCategory.code === 'EMPLOYEE' ? 'fullName' : 'venNickname'}
            onChange={innerValue => onChange({ partnerId: innerValue })}
            value={value.partnerId}
          />
        </Col>
      </Row>
    )}
  </WrapFormItem>,
  hasValue,
  parser: value => hasValue(value) ? {
    partnerCategory: value.partnerCategory.code,
    partnerId: value.partnerId ? value.partnerId.map(item => item.id) : null
  } : null,
  tagParser: value => value.partnerCategory &&  value.partnerCategory.code === 'EMPLOYEE' ?
    (value.partnerId.length > 0 ? value.partnerId[0].fullName : messages('finance-4.key303')/*员工*/) :
    (value.partnerId.length > 0 ? value.partnerId[0].venNickname : messages('finance-4.key304')/*供应商*/)
}
